import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/routeHelper.dart';
import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';
import '../../helper/Helper.dart';
import '../../helper/channelHelper.dart';

import '../../helper/uidata.dart';

import 'shipuse_authorty.dart';

import '../../model/shipuse.dart';

class ShipUseInfoPage extends StatefulWidget {
  @override
  ShipUseInfoPage(this.s1, this.callback);
  final ShipUse s1;
  final RefrushCallback callback;

  @override
  createState() => new ShipUseInfoPageState();
}

class ShipUseInfoPageState extends State<ShipUseInfoPage> {
  ShipUse s1 = ShipUse(
      0,
      "Guid",
      "Title",
      "Department",
      "UseTime",
      "Content",
      "Members",
      "ZBYName",
      "ZBYPhone",
      "CreateTime",
      "BMName",
      "BMQM",
      "BMPhone",
      "BMTM",
      false,
      "HDQM",
      "HDTM",
      "HDUserID",
      false,
      "SYQM",
      "SYTM",
      "SYUserID",
      false,
      "LDQM",
      "LDTM",
      "LDUserID",
      false,
      "ManaName",
      "ManaPhone",
      "ManaName2",
      "CZName",
      "CZName2",
      "CZPhone",
      "ChuanBo",
      "ChuanBo2",
      "UseTime2",
      "UsePath2",
      0,
      0,
      "TianQi",
      "HangXing",
      "CunDang",
      "HZTM",
      "HZQM",
      "HZSHTM",
      "HZUserID",
      false,
      0,
      "Remark");
  final TextEditingController c2 = new TextEditingController();

  @override
  void initState() {
    super.initState();
    //s1 = widget.s1;
    //UIData.CurrentUser!.Authority="水调值班员,电厂值班员";
    loadData();
  }

  loadData() async {
    var s2 = await ShipUse.Get("ID=@ID", {"ID": widget.s1.ID});
    setState(() {
      s1 = s2 as ShipUse;
    });
  }

  upload(String imgId) async {
    // await Helper.GetstoragePermission();
    if (imgId == null) return;

    s1 = await ShipUse.Get("ID=@ID", {"ID": s1.ID}) as ShipUse;

    if (s1.State == 2 && s1.SYUserID == UIData.CurrentUser!.Guid) {
      s1.SYIfOK = true;
      s1.SYTM = Helper.DateTimeNowString();
      s1.SYQM = imgId;

      s1.State = 3;
      await ShipUse.SendById(s1.Title, "需要签字", s1.HDUserID);
    }

    if (s1.State == 3 && s1.HDUserID == UIData.CurrentUser!.Guid) {
      s1.HDIfOK = true;
      s1.HDTM = Helper.DateTimeNowString();
      s1.HDQM = imgId;

      s1.State = 4;
      await ShipUse.SendById(s1.Title, "需要签字", s1.LDUserID);
    }

    if (s1.State == 4 && s1.LDUserID == UIData.CurrentUser!.Guid) {
      s1.LDIfOK = true;
      s1.LDTM = Helper.DateTimeNowString();
      s1.LDQM = imgId;

      s1.State = 5;
      await ShipUse.SendByPhone(s1.Title, "已签发完成！", s1.ZBYPhone);
    }

    await ShipUse.Update(s1);
    await loadData();
    widget.callback();
    ChannelHelper.showToast("签字成功！");
  }

  List<Widget> getWidgetList3() {
    List<Widget> list1 = <Widget>[];
    list1.add(
        InputWidget.getUserTile2(context, "申请部门联系人", s1.ZBYName, s1.ZBYPhone));
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(
        InputWidget.getUserTile2(context, "申请部门负责人", s1.BMName, s1.BMPhone));
    list1.add(InputWidget.getQMTile(
        s1.BMIfOK, Helper.DateTimeFormat(s1.BMTM), s1.BMQM));
    if (!s1.BMIfOK) return list1;
    list1.add(Divider(
      height: 0.0,
    ));

    return list1;
  }

  List<Widget> getWidgetList() {
    List<Widget> list1 = <Widget>[];

/*
  list1.add(
        InputWidget.getUserTile2(context, "申请部门负责人", s1.BMName, s1.BMPhone));
    list1.add(InputWidget.getQMTile(
        s1.BMIfOK, Helper.DateTimeFormat(s1.BMTM), s1.BMQM));
*/

    list1.add(InputWidget.getQMTile2(
        "申请部门负责人", s1.BMIfOK, s1.BMQM, Helper.DateTimeFormat(s1.BMTM)));

    if (!s1.BMIfOK) return list1;
    list1.add(Divider(
      height: 0.0,
    ));

    list1.add(InputWidget.getQMTile2(
        "实业公司负责人", s1.SYIfOK, s1.SYQM, Helper.DateTimeFormat(s1.SYTM)));
    //list1.add(InputWidget.getUserTile(context, "实业公司负责人", s1.SYUserID));
    //list1.add(InputWidget.getQMTile(
    //   s1.SYIfOK, Helper.DateTimeFormat(s1.SYTM), s1.SYQM));

    if (!s1.SYIfOK) return list1;

    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.getQMTile2(
        "河道部负责人", s1.HDIfOK, s1.HDQM, Helper.DateTimeFormat(s1.HDTM)));
    //list1.add(InputWidget.getUserTile(context, "河道部负责人", s1.HDUserID));
    //list1.add(InputWidget.getQMTile(
    // s1.HDIfOK, Helper.DateTimeFormat(s1.HDTM), s1.HDQM));
    list1.add(Divider(
      height: 0.0,
    ));
    if (!s1.HDIfOK) return list1;

    //list1.add(InputWidget.getUserTile(context, "公司分管领导", s1.LDUserID));
    // list1.add(InputWidget.getQMTile(
    // s1.LDIfOK, Helper.DateTimeFormat(s1.LDTM), s1.LDQM));

    list1.add(InputWidget.getQMTile2(
        "公司分管领导", s1.LDIfOK, s1.LDQM, Helper.DateTimeFormat(s1.LDTM)));
    // list1.add(InputWidget.getQMTile(
    // s1.LDIfOK, Helper.DateTimeFormat(s1.LDTM), s1.LDQM));

    list1.add(Divider(
      height: 0.0,
    ));

    return list1;
  }

  List<Widget> getAllWidget(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    list1.add(InputWidget.inCard(ShipUseAuthority.GetShipTitle(s1)));

    list2.add(InputWidget.getInfo("用船部门", s1.Department));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo2("用船时段", s1.UseTime));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo2("用船事由", s1.Content));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo2("乘船人员", s1.Members));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(
        InputWidget.getUserTile2(context, "申请部门联系人", s1.ZBYName, s1.ZBYPhone));
    if (s1.State < 2) {
      list2.add(Divider(
        height: 0.0,
      ));
      list2.add(InputWidget.getQMTile(s1.BMIfOK, "申请部门负责人", s1.BMQM));
    }

    list1.add(InputWidget.inCard2(list2));

    if (s1.State < 2) return list1;

    list3.add(InputWidget.getInfo("拟安排船艇名称", s1.ChuanBo));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(
        InputWidget.getUserTile2(context, "船艇管理人员", s1.ManaName, s1.ManaPhone));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("船长、船员", s1.CZName));

    list1.add(InputWidget.inCard2(list3));

    list1.add(InputWidget.inCard2(getWidgetList()));
    //list1.add(InputWidget.getQMTile(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  void showPanel(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(
            upload, ShipUseAuthority.QianZiTitle(s1, UIData.CurrentUser!));
      },
    );
  }

  Widget getFloatingButton(BuildContext context) {
    if (s1 == null) return new Container();
    if (ShipUseAuthority.IfQianZi(s1, UIData.CurrentUser!)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.penAlt),
          onPressed: () {
            showPanel(context);
          });
    }

    if (s1.State == 1 && ShipUseAuthority.IfHuiZhi(UIData.CurrentUser!)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.penAlt),
          onPressed: () {
            RouteHelper.ShipUseGuanLiAdd(context, s1, () async {
              await loadData();
            });
          });
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // if (s1 == null) return Center(child:SpinKitCircle(color: Colors.blue,));

    // TODO: implement build
    return Scaffold(
        // appBar: AppBar(title: Text('工作票详情')),
        //bottomSheet:bottomPanel2() ,
        floatingActionButton: getFloatingButton(context),
        body: s1.ID == 0
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : SingleChildScrollView(
                child: new Container(
                    margin: EdgeInsets.only(top: 8.0),
                    child: new Column(children: getAllWidget(context))),
              ));
  }
}
